package com.canbot.u05.activity.training;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.training.Step;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;


/**
 * 培训主持模式------动作设置界面
 */

public class TrainingActionSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private String isUpdate;

        private RelativeLayout relativeLayoutHint;

        private RelativeLayout relativeLayoutCustom;

        private String actionId;

        private String mPicPath;

        private String path;

        private Boolean isTTS;

        private List<Step> stepList =new ArrayList<>();

        private SharedPreferences sp;

        private String taskName;

        private String oldActionId;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_action_setting);
                initView();

                sp = getSharedPreferences("training_host", Context.MODE_PRIVATE);

                isUpdate = getIntent().getStringExtra("isUpdate");
                mPicPath=getIntent().getStringExtra("mPicPath");
                path=getIntent().getStringExtra("path");
                isTTS=getIntent().getBooleanExtra("isTTS",false);
                taskName=getIntent().getStringExtra("taskName");
                oldActionId=getIntent().getStringExtra("oldActionId");

                if(! "-1".equals(oldActionId) && !TextUtils.isEmpty(isUpdate)){
                        actionId = oldActionId;
                        relativeLayoutHint.setBackgroundResource(R.drawable.training_action_bg);
                        relativeLayoutCustom.setBackgroundResource(R.drawable.action_hint_image_press);
                }
                if(!TextUtils.isEmpty(getIntent().getStringExtra("isFirst")) && TextUtils.isEmpty(isUpdate)){
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.TRAINING_DIR + "rwbzbzts_pxzc_u05_003.wav");
                        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "30");
                }
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        private void initView() {
                findViewById(R.id.bt_back).setOnClickListener(this);
                findViewById(R.id.bt_save).setOnClickListener(this);
                findViewById(R.id.rel_custom).setOnClickListener(this);
                findViewById(R.id.rel_hint).setOnClickListener(this);
                relativeLayoutHint = (RelativeLayout) findViewById(R.id.rel_hint);
                relativeLayoutCustom = (RelativeLayout) findViewById(R.id.rel_custom);
                actionId="-1";
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.bt_back:
                                if(TextUtils.isEmpty(isUpdate)){
                                        Intent intent = new Intent();
                                        intent.putExtra("actionId", actionId);
                                        setResult(RESULT_OK, intent);
                                }
                                finish();
                                break;
                        case R.id.bt_save:
                                if (actionId == null) {
                                        ToastTools.newInstance(this).showText(getString(R.string.please_select_action));
                                        return;
                                }
                                if(!TextUtils.isEmpty(isUpdate)){
                                        setResult(RESULT_OK, new Intent().putExtra("actionId",actionId));
                                        finish();
                                        return;
                                }
                                String steps;

                                if(TextUtils.isEmpty(taskName)){
                                        steps=sp.getString("step", null);
                                }else{
                                        steps=sp.getString(taskName, null);
                                }

                                Logger.e("TrainingChooseAudioActivity",steps+"之前position");

                                if(!TextUtils.isEmpty(steps)){
                                        stepList=JSON.parseArray(steps, Step.class);
                                }

                                stepList.add(stepList.size(),new Step(mPicPath,path,actionId,isTTS));

                                Logger.e("TrainingChooseAudioActivity",stepList.toString()+"之后position");

                                if(TextUtils.isEmpty(taskName)){
                                        sp.edit().putString("step", JSON.toJSONString(stepList)).apply();
                                }else{
                                        sp.edit().putString(taskName, JSON.toJSONString(stepList)).apply();
                                }


                                MainApplication.finishActivity(TrainingAudioSettingActivity.class.getName());
                                MainApplication.finishActivity(TrainingHostImageSettingActivity.class.getName());
                                finish();
                                break;
                        case R.id.rel_custom:
                                Intent intent1=new Intent(TrainingActionSettingActivity.this, TrainingChooseActionActivity.class);
                                intent1.putExtra("actionId",actionId);
                                startActivityForResult(intent1, 104);
                                break;
                        case R.id.rel_hint:
                                actionId="-1";
                                relativeLayoutCustom.setBackgroundResource(R.drawable.training_action_bg);
                                relativeLayoutHint.setBackgroundResource(R.drawable.action_hint_image_press);
                                break;
                }

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null) {
                        return;
                }
                if (requestCode == 104 && resultCode == RESULT_OK) {
                        relativeLayoutHint.setBackgroundResource(R.drawable.training_action_bg);
                        relativeLayoutCustom.setBackgroundResource(R.drawable.action_hint_image_press);
                        actionId=data.getStringExtra("actionId");
                }
        }
}
